import Request from '@/js_sdk/luch-request';
import Vue from 'vue';
const http = new Request();

http.config.header = {};
http.setConfig((config) => {
  /* 设置全局配置 */
  config.header = {
    ...config.header,
  };
  return config;
});

//请求前拦截，用来动态加参,例如token
http.interceptors.request.use(
  (config) => {
    // 可使用async await 做异步操作
    config.baseURL = '';
    config.header = {
      ...config.header,
      Accept: 'application/json, text/plain, */*',
    };
    return config;
  },
  (config) => {
    // 可使用async await 做异步操作
    return Promise.reject(config);
  },
);

// 请求后
http.interceptors.response.use(
  (response) => {
    return response;
  },
  (response) => {
    /*  对响应错误做点什么 （statusCode !== 200）*/
    //未登录时清空缓存跳转
    if (response.statusCode == 401) {
      // uni.clearStorageSync();
      uni.switchTab({
        url: '/pages/login/index',
      });
    }
    return Promise.reject(response);
  },
);
export default http;
